//
//  Printer.m
//  pa
//
//  Created by HaiJiao Chen on 12-12-24.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "Printer.h"
#import "MiniPrinterFunctions.h"
#import "nibLoader.h"
#import "AppDelegate.h"
#import "CHJAlertView.h"

static NSArray *printerArray = nil;

@implementation Printer
- (void)printText:(CHJAlertView*)alert
{
    NSString *portName = [AppDelegate getPortName];
    NSString *portSettings = [AppDelegate getPortSettings];
    
    bool underline = false;
    bool emphasized = false;
    bool upsidedown = false;
    bool inverColor = false;
    
    unsigned char heightExpansion = 0;
    unsigned char widthExpansion = 0;
    int leftMargin = 0;
    
    Alignment alignment = Left;
    NSData *textData = [textView.text dataUsingEncoding:NSWindowsCP1252StringEncoding];
    
    unsigned char *textBytes = (unsigned char*)malloc([textData length]);
    [textData getBytes:textBytes];
    
    [MiniPrinterFunctions PrintText:portName 
                       PortSettings:portSettings 
                          Underline:underline 
                         Emphasized:emphasized
                        Upsideddown:upsidedown
                        InvertColor:inverColor
                    HeightExpansion:heightExpansion
                     WidthExpansion:widthExpansion
                         LeftMargin:leftMargin
                          Alignment:alignment
                        TextToPrint:textBytes
                    TextToPrintSize:[textData length]];
    
    free(textBytes);
    [alert close];
}
- (IBAction)printTextFormating
{
    CHJAlertView* alert = [[CHJAlertView alloc] init];
    [alert setLoadingText:@"printing" animated:NO];
    [alert show];
    [self performSelector:@selector(printText:) withObject:alert afterDelay:0];
    [alert release];
}

- (void)selectFirst
{
    if ([printerArray count] > 0)
    {
        PortInfo *portInfo = [printerArray objectAtIndex:0];
        [AppDelegate setPortName:portInfo.portName];
        
        NSString *localPortSettings = @"mini";
        
        [AppDelegate setPortSettings:localPortSettings];
        
        uilabel_portname.text = portInfo.portName;
    }
}
- (IBAction)pushButtonSearch:(id)sender
{
    [printerArray release];
    printerArray = [SMPort searchPrinter];
    [printers reloadData];
    [self selectFirst];
    printers.hidden = NO;
    self.hidden = NO;
}

- (IBAction)back:(id)sender
{
    [self removeFromSuperview];
}



-(void)viewLoadFromNib:(NSString*)printContent
{
    textView.text = printContent;
    if (printerArray == nil)
    {
        [self performSelector:@selector(pushButtonSearch:) withObject:nil afterDelay:0];
        self.hidden = YES;
    }
    else
    {
        [self selectFirst];
    }
    [printers reloadData];
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{    
    return printerArray.count+1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    
    if (indexPath.row < printerArray.count) {
        PortInfo *port = [printerArray objectAtIndex:indexPath.row];
        cell.textLabel.text = port.modelName;
        NSString *detailText = [NSString stringWithFormat:@"%@(%@)", port.portName, port.macAddress];
        cell.detailTextLabel.text = detailText;
    } else {
        cell.textLabel.text = @"Back";
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row < printerArray.count)
    {
        PortInfo *portInfo = [printerArray objectAtIndex:indexPath.row];
        [AppDelegate setPortName:portInfo.portName];
        
        NSString *localPortSettings = @"mini";
        
        [AppDelegate setPortSettings:localPortSettings];
        
        uilabel_portname.text = portInfo.portName;
    }
    printers.hidden = YES;
}

@end
